<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
// use app\filters\AccessFilter;

class ImportController extends Controller
{   
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'login' => ['class' => \app\filters\LoginFilter::className()],
            // 'access' => [
            //     'class' => AccessFilter::className(),
            //     'actions' => [
            //         'actionList' => [AccessFilter::MODULE_UUID_MENU, 'list'],
            //         'actionGetAddContent' => [AccessFilter::MODULE_UUID_MENU, 'add-update-c'],
            //         'actionAdd' => [AccessFilter::MODULE_UUID_MENU, 'add-update'],
            //         'actionDelete' => [AccessFilter::MODULE_UUID_MENU, 'del'],
            //     ],
            // ],
        ];
    }

    private $base_dist_dir = 'D:\\forgame_code\\ChainInformation\\src\\modules\\home\\';
    private $base_source_dir = 'D:\\forgame文档\\ChainInformation\\产品\\区块链网站翻译0727 Joe.xlsx';

    //暂时不支持的语言 留空即可
    private $LANG = [
        1 => 'zh-cn',
        2 => 'en-us',   
    ];


    //导入多语言 
    public function actionTranslate(){
        $base_dir = iconv("UTF-8","GB2312//IGNORE",$this->base_source_dir) ; 

        $dist_dir = $this->base_dist_dir.'languages\\src\\';
        $dist_dir = iconv("UTF-8","GB2312//IGNORE",$dist_dir) ; 

        require Yii::getAlias("@web").'../helpers/PHPExcel.php';
        $excelReader = \PHPExcel_IOFactory::createReader('Excel2007');//xslx格式的'

        foreach ($this->LANG as $key => $value) {
            $file = $base_dir ;

            $phpexcel = $excelReader->load($file);//载入文件并获取第一个sheet
            $sheetCount = $phpexcel->getSheetCount();

            for ($i = 0; $i < $sheetCount; $i++) {
                $sheet = $phpexcel->getSheet($i);
                switch ($i) {
                    //前端文字
                    case 0:
                        $this->format_1($sheet, $dist_dir,$key);
                        break;
                    default:
                        # code...
                        break;
                }
            } 
        }  
    }

    //每张表定义一个导出
    private function format_1($sheet, $base_dir,$key){
        $return_array = [];

        $total_line = $sheet->getHighestRow();
        for ($row = 2; $row <= $total_line; $row++) {
            $columnInfo = $countries = array();
            for ($column = 'A'; $column <= 'C'; $column++) {
                $columnInfo[] = trim($sheet->getCell($column . $row)->getValue());
            }
            if (!trim($columnInfo[0])) continue;
        
            $return_array[intval($columnInfo[0])] = $columnInfo[$key];
            
        }
        $str = '<?php return ';
        $str .= var_export($return_array,TRUE);
        $str .= ';';
        if(!is_dir($base_dir .= '\\'.$this->LANG[$key])) mkdir($base_dir);
        file_put_contents($base_dir.'/common.php',$str);
    }         

    //一次导入所有需要的
    public function actionStart(){
        $st_time = microtime(true);
        $this->actionTranslate();
        $end_time = microtime(true);
        echo '导入成功,耗时'. round($end_time-$st_time, 6).'秒';  
    }
   
}
